<?php

App::uses('AppController', 'Controller');

/**
 * Properties Controller
 *
 * @property Property $Property
 */
class PropertiesController extends AppController {

    public function beforeFilter() {
        Controller::loadModel('Parameter');
        $this->set('constraints', $this->Parameter->getPropertyConstraint()); // status array of users
    }

    /**
     * index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Property->recursive = 0;
        $this->set('properties', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->Property->id = $id;
        if (!$this->Property->exists()) {
            throw new NotFoundException(__('Invalid property'));
        }
        $this->set('property', $this->Property->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function admin_add() {
        if ($this->request->is('post')) {
            $this->Property->create();
            if (isset($this->request->data['Property']['constraint'])) {
                $this->request->data['Property']['constraint'] = implode(";", $this->request->data['Property']['constraint']);
            }

            $this->request->data['Property']['created_at'] = date("Y-m-d H:i:s");
            $this->request->data['Property']['updated_at'] = date("Y-m-d H:i:s");
            $this->request->data['Property']['created_by'] = $this->Auth->user('id');

            if ($this->Property->save($this->request->data)) {
                $this->Session->setFlash(__('The property has been saved.'), 'default', array(), 'good');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The property could not be saved. Please, try again.'), 'default', array(), 'bad');
            }
        }
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->Property->id = $id;
        if (!$this->Property->exists()) {
            throw new NotFoundException(__('Invalid property'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if (isset($this->request->data['Property']['constraint'])) {
                $this->request->data['Property']['constraint'] = implode(";", $this->request->data['Property']['constraint']);
            }

            $this->request->data['Property']['updated_at'] = date("Y-m-d H:i:s");
            $this->request->data['Property']['created_by'] = $this->Auth->user('id');


            if ($this->Property->save($this->request->data)) {
                $this->Session->setFlash(__('The property has been saved'), 'default', array(), 'good');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The property could not be saved. Please, try again.'), 'default', array(), 'bad');
            }
        } else {
            $this->request->data = $this->Property->read(null, $id);
        }
    }

    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Property->id = $id;
        if (!$this->Property->exists()) {
            throw new NotFoundException(__('Invalid property'));
        }
        if ($this->Property->delete()) {
            $this->Session->setFlash(__('Property deleted'), 'default', array(), 'good');
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Property was not deleted'), 'default', array(), 'bad');
        $this->redirect(array('action' => 'index'));
    }

    public function admin_updateStatus(){
        $this->Property->id = $_POST['id'];
        if (!$this->Property->exists()) {
            throw new NotFoundException(__('Invalid post'));
        }
        if ($this->request->is('post')) {
            $this->request->data['Property']['status'] = $_POST['status'];
            if ($this->Property->save($this->request->data)) {
                $this->Session->setFlash(__('The post has been saved'), 'default', array(), 'good');
            } else {
                $this->Session->setFlash(__('The post could not be saved. Please, try again.'), 'default', array(), 'bad');
            }
        }

        $this->Property->recursive = 0;
        $this->set('properties', $this->paginate());
        $this->render('/Elements/Backend/property_list', 'ajax');
    }
}
